package com.ssy.lingxi.transaction.controller.mobile;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.transaction.api.dto.request.*;
import com.ssy.lingxi.transaction.api.dto.response.*;
import com.ssy.lingxi.transaction.api.enums.InquiryExternalStatusEnum;
import com.ssy.lingxi.transaction.service.IInquiryService;
import com.ssy.lingxi.transaction.service.IQuoteService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @menu 交易能力-询价报价-APP
 * @author wrc
 * @date 2020/7/21
 */
@RestController
@RequestMapping("/transaction/mobile")
public class MobileQuoteController extends BaseController {

    @Resource
    IQuoteService quoteService;

    @Resource
    IInquiryService inquiryService;

    /**
     * 询价报价-询价单查询
     */
    @GetMapping("/product/inquiry/list")
    public Wrapper<PageData<QuoteListResponse>> productInquiryList(InquiryListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return inquiryService.productInquiryList(request,sysUser);
    }

    /**
     *询价报价-询价单详情
     */
    @GetMapping("/product/inquiry/details")
    public Wrapper<InquiryListDetailsResponse> inquiryListDetails(InquiryListDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.inquiryListDetails(request,sysUser);
    }

    /**
     *询价报价-报价单商品列表查询-分页
     */
    @GetMapping("/inquiry/details")
    public Wrapper<PageData<InquiryListProductResponse>> inquiryDetails(InquiryListProductPageRequest request) {
        return inquiryService.inquiryDetails(request);
    }

    /**
     * 询价报价-报价单列表查询
     */
    @GetMapping("/product/quotation/list")
    public Wrapper<PageData<ProductQuotationListResponse>> productQuotationList(ProductQuotationListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationList(request,sysUser);
    }

    /**
     * 询价报价-报价单详情
     */
    @GetMapping("/product/quotation/details")
    public Wrapper<ProductQuotationDetailsResponse> productQuotationDetails(ProductQuotationDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationDetails(request,sysUser);
    }

    /**
     * 询价报价-新增询价报价单
     */
    @PostMapping("/product/quotation/add")
    public Wrapper productQuotationAdd(@Valid @RequestBody ProductQuotationAddRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationAdd(request,sysUser);
    }

    /**
     * 询价报价-修改询价报价单
     */
    @PostMapping("/product/quotation/update")
    public Wrapper productQuotationUpdate(@Valid @RequestBody ProductQuotationUpdateRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationUpdate(request,sysUser);
    }

    /**
     * 询价报价-删除询价报价单
     */
    @PostMapping("/product/quotation/delete")
    public Wrapper productQuotationDelete(@Valid @RequestBody ProductQuotationDeleteRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationDelete(request,sysUser);
    }

    /**
     * 询价报价-批量删除询价报价单
     */
    @PostMapping("/product/quotation/delete/all")
    public Wrapper productQuotationDeleteAll(@Valid @RequestBody ProductQuotationDeleteAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationDeleteAll(request,sysUser);
    }

    /**
     * 询价报价-提交审核
     */
    @PostMapping("/product/quotationt/submit")
    public Wrapper productQuotationSubmit(@Valid @RequestBody   ProductQuotationSubmitRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationSubmit(request,sysUser);
    }

    /**
     * 询价报价-批量提交审核
     */
    @PostMapping("/product/quotationt/all")
    public Wrapper productQuotationSubmitAll(@Valid  @RequestBody  ProductQuotationSubmitAllRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationSubmitAll(request,sysUser);
    }

    /**
     * 询价报价-待审核报价单列表
     */
    @GetMapping("/audit/product/quotation/list")
    public Wrapper<PageData<ProductQuotationListResponse>> auditProductQuotationList(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.auditProductQuotationList(request,sysUser);
    }

    /**
     * 询价报价-待新增报价单列表
     */
    @GetMapping("/stay/product/quotation/list")
    public Wrapper<PageData<ProductQuotationListResponse>> stayProductQuotationList(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.stayProductQuotationList(request,sysUser);
    }

    /**
     * 询价报价-待审核报价单
     */
    @PostMapping("/product/quotationt/audit/submit")
    public Wrapper productQuotationAuditSubmit(@Valid @RequestBody   ProductQuotationSubmitRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationAuditSubmit(request,sysUser);
    }

    /**
     * 询价报价-批量审核报价单
     */
    @PostMapping("/product/quotationt/audit/all")
    public Wrapper productQuotationAuditSubmitAll(@Valid  @RequestBody  ProductQuotationSubmitAllRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationAuditSubmitAll(request,sysUser);
    }

    /**
     * 询价报价-待新增报价单列表查询
     */
    @GetMapping("/audit/product/quotation/list/two")
    public Wrapper<PageData<ProductQuotationListResponse>> auditProductQuotationListTwo(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.auditProductQuotationListTwo(request,sysUser);
    }

    /**
     * 询价报价-待审核报价单（二级）
     */
    @PostMapping("/product/quotationt/audit/submit/two")
    public Wrapper productQuotationAuditSubmitTwo(@Valid @RequestBody   ProductQuotationSubmitRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationAuditSubmitTwo(request,sysUser);
    }

    /**
     * 询价报价-批量审核报价单（二级）
     */
    @PostMapping("/product/quotationt/audit/all/two")
    public Wrapper productQuotationAuditSubmitAllTwo(@Valid  @RequestBody  ProductQuotationSubmitAllRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationAuditSubmitAllTwo(request,sysUser);
    }

    /**
     * 询价报价-待提交报价单列表
     */
    @GetMapping("/to/submit/product/quotation/list")
    public Wrapper<PageData<ProductQuotationListResponse>> toSubmitProductQuotationList(ProductQuotationListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.toSubmitProductQuotationListTwo(request,sysUser);
    }

    /**
     * 商品询价-提交报价单
     */
    @PostMapping("/product/quotationt/to/submit")
    public Wrapper productQuotationToSubmit(@Valid @RequestBody   ProductQuotationSubmitRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationToSubmit(request,sysUser);
    }

    /**
     * 询价报价-批量审核报价单
     */
    @PostMapping("/product/quotationt/to/submit/all")
    public Wrapper productQuotationToSubmitAll(@Valid  @RequestBody  ProductQuotationSubmitAllRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.productQuotationToSubmitAll(request,sysUser);
    }

    /**
     * 询价报价-查询对应询价单号
     */
    @GetMapping("/corresponding/inquiry/number")
    public Wrapper<PageData<CorrespondingInquiryNumberResponse>> correspondingInquiryNumber(CorrespondingInquiryNumberRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.correspondingInquiryNumber(request,sysUser);
    }

    /**
     * 询价报价-查询报价单商品详情列表-分页
     */
    @GetMapping("/quotationt/product/list")
    public Wrapper<PageData<QuotationtProductListResponse>> quotationtProductList(QuotationtProductListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return quoteService.quotationtProductList(request,sysUser);
    }

    /**
     * 询价报价-内部工作流程枚举
     */
    @PostMapping("/quotationt/inquiry/interior/state/enum")
    private Wrapper<List<QuotationExternalStateEnumResponse>> getQuotationtInquiryInteriorStateEnum() {
        return quoteService.getQuotationtInquiryInteriorStateEnum();
    }

    /**
     * 询价报价-外部工作流程枚举
     */
    @GetMapping("/quotationt/inquiry/external/state/enum")
    private Wrapper<List<QuotationExternalStateEnumResponse>> getQuotationtInquiryExternalEnum() {
        return Wrapper.success(Arrays.stream(InquiryExternalStatusEnum.values()).map(r -> {
            if(!InquiryExternalStatusEnum.INQUIRY_SHEET_TO_BE_SUBMITTED.getState().equals(r.getState())){
                QuotationExternalStateEnumResponse status = new QuotationExternalStateEnumResponse();
                status.setState(r.getState());
                status.setName(r.getName());
                return status;
            }else{
                return null;
            }
        }).filter(Objects::nonNull).collect(Collectors.toList()));
    }

//    /**
//     * 询价报价-内部查询报价单商品详情
//     */
//    @GetMapping("/inquiry/product/price")
//    private Wrapper<List<InquiryListProductResponse>> inquiryProductPrice(@RequestParam(name = "quotationNo")String quotationNo) {
//        return quoteService.inquiryProductPrice(quotationNo);
//    }
}
